/*!
 * Page: pages/topic/member/Member
 * License: MIT
 * Created: 2019-11-19 11:50
 */

import { topicApi } from '/api/topic'
import { $event } from '/utils'

Page({
  data: {
    user: {},
    id: 0,
    list: [] as DomoUser[],
    isLastPage: false,
    member: 0,
    iscreater: false as boolean,
    logs: [] as TopicLog[],
  },
  async onLoad(query: any) {
    const id = +query.id
    const iscreater = query.iscreater === 'true'
    this.setData({
      id,
      iscreater,
    })
    if (iscreater) {
      const logs = await topicApi.needAudited(id, { offset: 0, limit: 100, status: 'waiting' })
      this.setData({
        logs,
      })
    }
    this.getLastpageData()
    this.getMember()
  },
  /**
   * return void
   */
  onReachBottom() {
    if (this.data.isLastPage) {
      wx.showToast({ title: '已没有更多', icon: 'none', duration: 1000 })
    } else {
      this.getMember()
    }
  },
  async getMember() {
    wx.showLoading({
      title: '加载中',
    })
    const list = await topicApi.getTopicMember({
      id: this.data.id,
      first_creator: 1,
      limit: 40,
      after: this.data.list.length > 0 ? this.data.list[this.data.list.length - 1].id : 0,
    })
    this.setData({
      list: [...this.data.list, ...list],
      isLastPage: list.length < 40,
    })
    wx.hideLoading()
  },
  getLastpageData() {
    const pages = getCurrentPages()
    const lastpage = pages[pages.length - 2]
    const member = lastpage.data.topic.followers_count
    this.setData({
      member,
    })
  },
  /** 审核 */
  toExamine(e: any) {
    const {
      currentTarget: {
        dataset: { logid, index },
      },
    } = e
    wx.showActionSheet({
      itemList: ['同意加入', '拒绝加入'],
      success: async (res) => {
        switch (res.tapIndex) {
          case 0:
            topicApi
              .agreeJoin(this.data.id, logid)
              .then(() => {
                wx.showToast({
                  title: '操作成功',
                })
                const logs = this.data.logs
                const list = this.data.list
                list.push(logs.splice(index, 1)[0].user)
                this.setData({
                  logs,
                  list,
                })
                $event.emit('after:operation', true)
              })
              .catch(() => {
                wx.showToast({
                  title: '操作失败,请稍后重试',
                  icon: 'none',
                })
              })

            break
          case 1:
            topicApi
              .refuseJoin(this.data.id, logid)
              .then(() => {
                wx.showToast({
                  title: '操作成功',
                })
                const logs = this.data.logs
                logs.splice(index, 1)
                this.setData({
                  logs,
                })
                $event.emit('after:operation', false)
              })
              .catch(() => {
                wx.showToast({
                  title: '操作失败,请稍后重试',
                  icon: 'none',
                })
              })
            break
        }
      },
    })
  },
  /** 邀请成员 */
  invite() {
    wx.navigateTo({
      url: '/pages/topic/invite/invite',
    })
  },
})
